local nya__qicai = fk.CreateSkill {

  name = "nya__qicai",

  tags = { Skill.Compulsory, },

}



nya__qicai:addEffect("targetmod", {
  name = "nya__qicai",
  frequency = Skill.Compulsory,
  bypass_distances = function(self, player, skill, card)
    return player:hasSkill(nya__qicai.name) and card and card.type == Card.TypeTrick
  end,
})

nya__qicai:addEffect(fk.BeforeCardsMove, {
  name = "#nya__qicai_trigger",
  mute = true,
  events = {fk.BeforeCardsMove, fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__qicai.name) then
      if event == fk.BeforeCardsMove then
        if player:getEquipment(Card.SubtypeArmor) then
          for _, move in ipairs(data) do
            if move.from==player and move.moveReason == fk.ReasonDiscard and move.proposer ~= player.id then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerEquip and info.cardId == player:getEquipment(Card.SubtypeArmor) then
                  return true
                end
              end
            end
          end
        end
      else
        return data.name == "nya__play" and target ~= player and player.room:getTag("RoundCount")
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("nya__qicai")
    if event == fk.BeforeCardsMove then
      room:notifySkillInvoked(player, "nya__qicai", "defensive")
      local ids = {}
      for _, move in ipairs(data) do
        if move.from==player and move.moveReason == fk.ReasonDiscard and move.proposer ~= player.id then
          local move_info = {}
          for _, info in ipairs(move.moveInfo) do
            local id = info.cardId
            if info.fromArea == Card.PlayerEquip and  Fk:getCardById(id).sub_type == Card.SubtypeArmor then
              table.insert(ids, id)
            else
              table.insert(move_info, info)
            end
          end
          if #ids > 0 then
            move.moveInfo = move_info
          end
        end
      end
      if #ids > 0 then
        player.room:sendLog{
          type = "#cancelDismantle",
          card = ids,
          arg = nya__qicai.name,
        }
      end
    else
      room:notifySkillInvoked(player, "nya__qicai", "drawcard")
      local card = room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #card > 0 then
        room:moveCards({
          ids = card,
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonPrey,
          proposer = player.id,
          skillName = "nya__qicai",
        })
      end
    end
  end,
})
nya__qicai:addEffect(fk.EventAcquireSkill, {
  name = "#nya__qicai_trigger",
  mute = true,
  events = {fk.BeforeCardsMove, fk.EventAcquireSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__qicai.name) then
      if event == fk.BeforeCardsMove then
        if player:getEquipment(Card.SubtypeArmor) then
          for _, move in ipairs(data) do
            if move.from==player and move.moveReason == fk.ReasonDiscard and move.proposer ~= player.id then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerEquip and info.cardId == player:getEquipment(Card.SubtypeArmor) then
                  return true
                end
              end
            end
          end
        end
      else
        return data.name == "nya__play" and target ~= player and player.room:getTag("RoundCount")
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("nya__qicai")
    if event == fk.BeforeCardsMove then
      room:notifySkillInvoked(player, "nya__qicai", "defensive")
      local ids = {}
      for _, move in ipairs(data) do
        if move.from==player and move.moveReason == fk.ReasonDiscard and move.proposer ~= player.id then
          local move_info = {}
          for _, info in ipairs(move.moveInfo) do
            local id = info.cardId
            if info.fromArea == Card.PlayerEquip and  Fk:getCardById(id).sub_type == Card.SubtypeArmor then
              table.insert(ids, id)
            else
              table.insert(move_info, info)
            end
          end
          if #ids > 0 then
            move.moveInfo = move_info
          end
        end
      end
      if #ids > 0 then
        player.room:sendLog{
          type = "#cancelDismantle",
          card = ids,
          arg = nya__qicai.name,
        }
      end
    else
      room:notifySkillInvoked(player, "nya__qicai", "drawcard")
      local card = room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #card > 0 then
        room:moveCards({
          ids = card,
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonPrey,
          proposer = player.id,
          skillName = "nya__qicai",
        })
      end
    end
  end,
})

return nya__qicai